package com.itrui.commodity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itrui.commodity.common.CustomException;
import com.itrui.commodity.dto.ProductDto;
import com.itrui.commodity.pojo.Product;
import com.itrui.commodity.pojo.ProductLabel;
import com.itrui.commodity.service.ProductLabelService;
import com.itrui.commodity.service.ProductService;
import com.itrui.commodity.mapper.ProductMapper;
import com.itrui.feign.client.PictureClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author 蕊蕤的小腿肚子
* @description 针对表【product(商品管理)】的数据库操作Service实现
* @createDate 2022-08-17 19:12:17
*/
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product>
    implements ProductService{

    @Autowired
    private ProductLabelService productLabelService;

    @Autowired
    private PictureClient pictureClient;
    /**
     * 新增商品，同时保存对应的标签数据
     * @param productDto
     */
    @Transactional
    public void saveWithLabel(ProductDto productDto) {

        //保存商品图片到图片表
        List<String> uri = productDto.getPicture().stream().map((item) -> {
            return item.getUri();
        }).collect(Collectors.toList());
        pictureClient.savePicture(uri, 1, productDto.getId());

        //保存商品的基本信息到商品表product
        this.save(productDto);

        Long productId = productDto.getId();//商品id

        //商品标签
        List<ProductLabel> labels = productDto.getLabels();
        labels = labels.stream().map((item) -> {
            item.setProductId(productId);
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味数据到菜品口味表Product_Label
        productLabelService.saveBatch(labels);

    }
    /**
     * 根据id查询菜品信息和对应的口味信息
     * @param id
     * @return
     */
    public ProductDto getByIdWithLabel(Long id) {
        //查询菜品基本信息，从Product表查询
        Product Product = this.getById(id);

        ProductDto ProductDto = new ProductDto();
        BeanUtils.copyProperties(Product,ProductDto);

        //查询当前菜品对应的口味信息，从Product_Label表查询
        LambdaQueryWrapper<ProductLabel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductLabel::getProductId,Product.getId());
        List<ProductLabel> Labels = productLabelService.list(queryWrapper);
        ProductDto.setLabels(Labels);

        return ProductDto;
    }

    @Override
    @Transactional
    public void updateWithLabel(ProductDto ProductDto) {
        //更新Product表基本信息
        this.updateById(ProductDto);

        //清理当前菜品对应口味数据---Product_Label表的delete操作
        LambdaQueryWrapper<ProductLabel> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(ProductLabel::getProductId,ProductDto.getId());

        productLabelService.remove(queryWrapper);

        //添加当前提交过来的口味数据---Product_Label表的insert操作
        List<ProductLabel> Labels = ProductDto.getLabels();

        Labels = Labels.stream().map((item) -> {
            item.setProductId(ProductDto.getId());
            return item;
        }).collect(Collectors.toList());

        productLabelService.saveBatch(Labels);
    }

    /**
     * 删除商品，同时需要删除商品和标签的关联数据
     * @param ids
     */
    @Transactional
    public void removeWithLabels(List<Long> ids) {
        //检查商品状态
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Product::getId, ids);
        queryWrapper.eq(Product::getStatus, 1);
        int count = this.count(queryWrapper);


        if (count > 0){
            //删除的商品在售，不能删除，抛出异常
            throw new CustomException("商品正在售卖中，不能删除");
        }

        //可以删除
        this.removeByIds(ids);


        //删除商品标签关系表中相关数据
        LambdaQueryWrapper<ProductLabel> productLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        productLabelLambdaQueryWrapper.in(ProductLabel::getProductId, ids);
        productLabelService.remove(productLabelLambdaQueryWrapper);

    }
}




